package com.is2.patchinglife.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.is2.patchinglife.R;
import com.is2.patchinglife.manager.ActivityManager;
import com.is2.patchinglife.manager.FacebookManager;
import com.is2.patchinglife.manager.LocalDataManager;
import com.is2.patchinglife.manager.RemoteDataManager;

public class LoginActivity extends Activity{
	
	//Coding facebook
    //Helper che simula il ciclo di vita dell'activity per la session di log con fb
    private UiLifecycleHelper uiHelper;
    
    //On state change session this callback will call
   private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            FacebookManager.logoutFacebook();
        }
    };
	

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// shows activity interface
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// Add activity to ActivityManager
		ActivityManager.getInstance().addActivity("LoginActivity", this);
		
		LocalDataManager.setContext(getApplicationContext());
		
		/** Login Listener **/
		FrameLayout stdLogin = (FrameLayout) findViewById(R.id.loginBtn);
		stdLogin.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				
				// Get inserted data
				EditText userText = (EditText) findViewById(R.id.txt_login_username);
				EditText pswText = (EditText) findViewById(R.id.txt_login_password);
				
				String user = userText.getText().toString();
				String psw = pswText.getText().toString();
				
				// Check Validity. Alert if not
				if(user.equals("") || psw.equals("")){
					AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
					builder.setTitle("ALERT");
					builder.setMessage("Invalid username and/or password.");
					builder.setCancelable(true);
					builder.setPositiveButton("OK", null);
					AlertDialog alert=builder.create();
					alert.show();
				
				}else if(RemoteDataManager.isOnline(LoginActivity.this)){
						toggleLoading();
						RemoteDataManager.authenticate(user,psw);
						
						
				}
				
			}
		});

		/** Skip Listener **/
		FrameLayout skipLogin = (FrameLayout) findViewById(R.id.skipBtn);
		skipLogin.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
				builder.setTitle("ALERT");
				builder.setMessage("Some functionalities will be inhibited without authentication. You can "+
								   "authenticate in every moment from Profile > Authenticate.");
				builder.setPositiveButton("SKIP",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						Intent menu_intent = new Intent(LoginActivity.this,MainMenuActivity.class);
						LoginActivity.this.startActivity(menu_intent);
					}
				});
				builder.setNegativeButton("CANCEL",null);
				AlertDialog alert=builder.create();
				alert.show();
			}
		});
		
		/** Register Listener **/
		FrameLayout regLogin = (FrameLayout) findViewById(R.id.registerBtn);
		regLogin.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				Intent signup_intent = new Intent(LoginActivity.this,SignUpActivity.class);
				LoginActivity.this.startActivity(signup_intent);
			}
		});
		
		
		uiHelper = new UiLifecycleHelper(this, callback);
	    uiHelper.onCreate(savedInstanceState);
	    FacebookManager.logoutFacebook();
	    
	    
	}
	
	public void toggleLoading(){
		//get refernces
		FrameLayout userContainer = (FrameLayout) findViewById(R.id.txt_login_username).getParent();
		FrameLayout pswContainer = (FrameLayout) findViewById(R.id.txt_login_password).getParent();
		RelativeLayout btnContainer = (RelativeLayout) findViewById(R.id.loginBtn).getParent();
		ProgressBar ProgressBar = (ProgressBar) findViewById(R.id.LoginProgressBar);
		
		//invert visibility: show progress bar & hide form or viceversa
		if(ProgressBar.getVisibility() == View.GONE){
			userContainer.setVisibility(View.GONE);
			pswContainer.setVisibility(View.GONE);
			btnContainer.setVisibility(View.GONE);
			ProgressBar.setVisibility(View.VISIBLE);
		}else{
			userContainer.setVisibility(View.VISIBLE);
			pswContainer.setVisibility(View.VISIBLE);
			btnContainer.setVisibility(View.VISIBLE);
			ProgressBar.setVisibility(View.GONE);
		}
		
	}
	
	@Override
	public void onDestroy(){
		String[] aList = {"LoginActivity"};
		ActivityManager.getInstance().closeActivities(aList,false);

		uiHelper.onDestroy();
		super.onDestroy();

	}
	
	@Override
	public void onResume(){
		super.onResume();
		uiHelper.onResume();
	}
	
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
    
}
